package com.xm.xmapp;

import java.util.ArrayList;
import java.util.List;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.TextView;
import android.widget.Toast;

import com.xm.xmapp.adapter.ImageGridAdapter;
import com.xm.xmapp.app.CodeConstant;
import com.xm.xmapp.app.MyAppcation;
import com.xm.xmapp.app.Type.A_ImageBucketChoose;
import com.xm.xmapp.app.Type.F_CarFeedBack;
import com.xm.xmapp.app.Type.F_PileFeedBack;
import com.xm.xmapp.base.BaseActivity;
import com.xm.xmapp.fragment.CarFeedBackFragment;
import com.xm.xmapp.fragment.PileFeedBackFragment;
import com.xm.xmapp.response.bean.CustomImageItem;

/**
 * 图片选择
 * 
 */
public class ImageChooseActivity extends BaseActivity {
	private List<CustomImageItem> mDataList = new ArrayList<CustomImageItem>();
	private String mBucketName;
	private int availableSize;
	private GridView mGridView;
	private int flag;

	private ImageGridAdapter mAdapter;
	private TextView mFinishBtn;
	private List<CustomImageItem> selectedImgs = new ArrayList<CustomImageItem>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_image_choose);

	}

	public void initView() {
		mDataList = (List<CustomImageItem>) getIntent().getSerializableExtra(CodeConstant.EXTRA_IMAGE_LIST);
		if (mDataList == null)
			mDataList = new ArrayList<CustomImageItem>();
		mBucketName = getIntent().getStringExtra(CodeConstant.EXTRA_BUCKET_NAME);
		flag = getIntent().getIntExtra("flag", 0);

		if (TextUtils.isEmpty(mBucketName)) {
			mBucketName = "请选择";
		}
		availableSize = getIntent().getIntExtra(CodeConstant.EXTRA_CAN_ADD_IMAGE_SIZE, CodeConstant.MAX_IMAGE_SIZE);
		mGridView = (GridView) findViewById(R.id.gridview);
		mGridView.setSelector(new ColorDrawable(Color.TRANSPARENT));
		mAdapter = new ImageGridAdapter(ImageChooseActivity.this, mDataList);
		mGridView.setAdapter(mAdapter);
		mFinishBtn = (TextView) findViewById(R.id.finish_btn);
		mFinishBtn.setText("完成" + "(" + selectedImgs.size() + "/" + availableSize + ")");
		mAdapter.notifyDataSetChanged();
	}

	private void initListener() {
		mFinishBtn.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {

				MyAppcation.getMyAppcation().notifyDataSetChanged(ImageBucketChooseActivity.class, A_ImageBucketChoose.DEFAULT);
				if (flag == 0) {
					MyAppcation.getMyAppcation().notifyDataSetChanged(CarFeedBackFragment.class, F_CarFeedBack.DEFAULT, selectedImgs);
				} else {
					MyAppcation.getMyAppcation().notifyDataSetChanged(PileFeedBackFragment.class, F_PileFeedBack.DEFAULT, selectedImgs);
				}
				finish();
			}

		});

		mGridView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

				CustomImageItem item = mDataList.get(position);
				if (item.isSelected) {
					item.isSelected = false;
					selectedImgs.remove(item);
				} else {
					if (selectedImgs.size() >= availableSize) {
						Toast.makeText(ImageChooseActivity.this, "最多选择" + availableSize + "张图片", Toast.LENGTH_SHORT)
								.show();
						return;
					}
					item.isSelected = true;
					selectedImgs.add(item);
				}
				mFinishBtn.setText("完成" + "(" + selectedImgs.size() + "/" + availableSize + ")");
				mAdapter.notifyDataSetChanged();
			}
		});
	}

	@Override
	public void initData() {
		setMyTitle("选择照片");
	}

	@Override
	public void initEvent() {
		initListener();
	}
}
